Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug logs to ASGIDispatch #371

Merged
merged 5 commits into from
Sep 23, 2019
Merged

Add debug logs to ASGIDispatch #371

merged 5 commits into from
Sep 23, 2019

Conversation

florimondmanca
Copy link
Member

@florimondmanca florimondmanca commented Sep 21, 2019

Working on and debugging #352, I noticed we didn't have debug logs for the ASGIDispatch, so here they are. :)

Example output:

import httpx

async def app(scope, receive, send):
    assert scope["type"] == "http"
    await send(
        {
            "type": "http.response.start",
            "status": 200,
            "headers": [[b"content-type", b"text/plain"]],
        }
    )
    await send({"type": "http.response.body", "body": b"Hello, world!"})

with httpx.Client(app=app) as client:
    r = client.get("http://app")
    print(r)
20:18:22.096 - httpx.dispatch.asgi - started
20:18:22.096 - httpx.dispatch.asgi - received message={'type': 'http.response.start', 'status': 200, 'headers': '<...>'}
20:18:22.096 - httpx.dispatch.asgi - received message={'type': 'http.response.body', 'body': '<13 bytes>'}
20:18:22.097 - httpx.dispatch.asgi - completed
<Response [200 OK]>

@sethmlarson
Copy link
Contributor

Since we show headers for the non-ASGI loggers, should we consider showing them here as well?

@florimondmanca
Copy link
Member Author

Good point @sethmlarson, which made me realize there were much more data we'd be interested in seeing. We now dump all of the ASGI scope and events, which yields something like this:

22:35:40.591 - httpx.dispatch.asgi - started type='http' asgi={'version': '3.0'} http_version='1.1' method='GET' headers=[(b'host', b'app'), (b'user-agent', b'python-httpx/0.7.3'), (b'accept', b'*/*'), (b'accept-encoding', b'gzip, deflate, br'), (b'connection', b'keep-alive')] scheme='https' path='/' query_string=b'' server='app' client=('127.0.0.1', 123) root_path=''
22:35:40.591 - httpx.dispatch.asgi - received type='http.response.start' status=200 headers=[[b'content-type', b'text/plain']]
22:35:40.591 - httpx.dispatch.asgi - received type='http.response.body' body='<13 bytes>'
22:35:40.594 - httpx.dispatch.asgi - completed

Copy link
Contributor

@sethmlarson sethmlarson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I just thought of we should refactor then this is good to go!

@StephenBrown2
Copy link
Contributor

Can this logger middleware be made more generic for requests, or is is specific to ASGI apps only?

@florimondmanca
Copy link
Member Author

@StephenBrown2 Its very much specific to ASGI. Our other loggers typically log h11/h2 events, I don’t see a way we can reconcile them with this. I hope having « ASGI » present in the name of the logger middleware (because it is an ASGI middleware, ie it implements the ASGI interface) conveys this intent well enough.

@StephenBrown2
Copy link
Contributor

Aye! HTTPX_DEBUG is good enough for everything, this just adds that logging to ASGI.

Copy link
Contributor

@sethmlarson sethmlarson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good after one comment!

httpx/utils.py Outdated Show resolved Hide resolved
@florimondmanca florimondmanca merged commit e62e5c3 into master Sep 23, 2019
@florimondmanca florimondmanca deleted the feat/asgi-debug-logs branch September 23, 2019 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants